/*
 * SimulationRun.java
 *
 * Created on Sobota, 2007, november 17, 19:20
 *
 */

package logic;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import logic.gridSimInterface.SimTags;
import logic.schedulers.AlgorithmCollection;
import logic.schedulers.AlgorithmPlugin;
import org.dom4j.DocumentException;
import xml.XmlStatistics;
import xml.XmlTopology;

/**
 * Staticka trieda, ktora vykonava jeden simulacny beh. Z argumentov
 * pri spusteni si zisti, z ktorych suborov ma nacitat topologiu
 * a parametre simulacie a do ktoreho suboru ma ulozit vysledky.
 *
 * @author alvin
 */
public class SimulationRun {
    
    /**
     * Pole argumentov obsahuje
     * <ul>
     * <li>cesta ku existujucemu suboru s topologiou
     * <li>cesta ku existujucemu suboru s parametrami simulacie
     * <li>cestu ku suboru, ktory sa vytvori a naplni vysledkami
     * </ul>
     */
    public static void main(String[] args) {
        
        System.out.println("SimulationRun.main()");
        
        if (args == null) {
            System.err.println("Missing arguments.");
            System.exit(1);
        }
        
        if (args.length != 3) {
            System.err.println("SimulationRun needs 3 arguments.");
            System.exit(1);
        }
            
        File topology = new File(args[0]);
        File parameters = new File(args[1]);
        File results = new File(args[2]);
        try {
            
            XmlTopology.loadTopology(topology.getAbsoluteFile().toURI().toURL().toString(), null);
        } catch (DocumentException ex) {
            ex.printStackTrace();
            System.exit(1);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(1);
        }
        
        try {
            SimulationParameters.loadFromFile(args[1]);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            System.exit(1);
        }
        
        System.out.println("local " + SimulationParameters.getLocalScheduler().getParametersText());
        System.out.println("global " + SimulationParameters.getGlobalScheduler().getParametersText());
        
        SimulationRunState stav = new SimulationRunState();
        try {
            JSimulation.start(stav);
        } catch (Exception ex) {
            ex.printStackTrace();
            System.exit(1);
        }
        
        try {
            
            XmlStatistics.saveSimulationRunResults(JSimulation.getStatistics(), args[2]);
        } catch (IOException ex) {
            ex.printStackTrace();
            System.exit(1);
        }
        
        System.exit(0);
    }
    
}
